﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls.Primitives;

namespace Themes
{
    public class CheckBoxGroup
    {
        public static bool GetIsEnabled(DependencyObject obj) => (bool)obj.GetValue(IsEnabledProperty);
        public static void SetIsEnabled(DependencyObject obj, string value) =>
                                                                  obj.SetValue(IsEnabledProperty, value);
        public static readonly DependencyProperty IsEnabledProperty =
             DependencyProperty.RegisterAttached("IsEnabled", typeof(bool), typeof(CheckBoxGroup),
                   new PropertyMetadata(false, Callback));

        private static void Callback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var container = d as UIElement;
            container.AddHandler(ToggleButton.CheckedEvent,
                                        new RoutedEventHandler(GroupedButton_Checked));
        }

        private static void GroupedButton_Checked(object sender, RoutedEventArgs e)
        {
            var container = sender as DependencyObject;
            var source = e.OriginalSource as ToggleButton;
            foreach (var child in LogicalTreeHelper.GetChildren(container).OfType<ToggleButton>())
            {
                if (child != source) child.IsChecked = false;
            }
        }
    }
}
